<?php 
YII::import('application.components.*');
YII::import('application.extensions.YiiMailer.YiiMailer');
class LoginController extends Controller{
	public $layout='//layouts/default';
	
	/**
	 *  @author wyl
	 *  @todo  Login
	 *  @date 20115.7.20
	 */	
	
	public function accessRules(){
	    return array(
	      array('deny',
            'actions'=>array('login'),
	        'users' =>array('@'),
	        'deniedCallback' => function(){
                Yii::app()->getRequest()->redirect(Yii::app()->baseUrl);
	        }
	      ),
	    );
	}
	
	public function actionIndex(){
		$this->breadcrumbs = array("LOGIN");
		if(!yii::app()->user->isGuest){
			header('location:'.Yii::app()->baseurl);
		}
	    $form=new FormAuth();
	    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
	    {
	        echo $form->validate();
	        Yii::app()->end();
	    }
	    if(!empty($_GET['url'])){

	    	Yii::app()->user->detail = Yii::app()->baseUrl."/".$_GET['url'];
	    	// var_dump(Yii::app()->user->detail);die;
	    }
	    if(!empty($_POST['FormAuth'])){
	        $form->attributes=$_POST['FormAuth'];
	        
	            LoginPlus::Login($form);
	           
	    }
	    $this->render('index',array("model"=>$form));
	}
		
	public function actionLogout(){
		Yii::app()->user->logout(true);
	}
	public function actionForgot(){
	    $form=new FormForgot();
	    if(isset($_POST['ajax']) && $_POST['ajax']==='forgot'){
	        echo CActiveForm::validate($form);
	        Yii::app()->end();
	    }
	    if(!empty($_POST)){
	        $customer=new Customer();
	        $customerinfo=$customer->find("email='".$_POST["FormForgot"]["email"]."'");
	        $forgot=new CustomerForgot();
	        $forgot_user=$forgot->findByPk($customerinfo->id);
	        if(!empty($forgot_user)){
	            $forgot_user->customer_id=$customerinfo->id;
	            $forgot_user->code=md5($customerinfo->id.time());
	            $forgot_user->created=time();
	            $forgot_user->live=time()+1800;
	            $forgot_user->save();
	        }else{
	            $forgot->customer_id=$customerinfo->id;
	            $forgot->code=md5($customerinfo->id.time());
	            $forgot->created=time();
	            $forgot->live=time()+600;
	            $forgot->save();
	        }   
	        $mailer = new YiiMailer();
	        $mailer->setView('forget_pwd');//调用模板
	        $mailer->setData(array('USER_NAME'=>$customerinfo->user_name,'email'=>$_POST['FormForgot']['email'],'code' => $forgot->code?$forgot->code:$forgot_user->code,'code_user' => md5($customerinfo->email)));//所需数据
	        //发送到邮箱
	        $mailer->AddAddress($customerinfo->email);//目标邮箱
	        
	        $mailer->Subject = Yii::t('demo', 'Your eGunner forgot Password request');//标题
	        if(!$mailer->Send()) {
	            echo "Mailer Error: " . $mailer->ErrorInfo;
	        }else{
	            echo "<script>location.href='".Yii::app()->baseUrl."/login/success';</script>";
	        }
	    }
	    $this->render('forgot',array("model" => $form));
	}
	public function actionCheckuser(){
	    $customer=new Customer();
	    $customerforgot=new CustomerForgot();
	    $customerforgotinfo=$customerforgot->find("code='".$_GET["code"]."'");
	    if(!empty($customerforgotinfo)){	    
    	    $customer=new Customer();
    	    $customerinfo=$customer->findByPk($customerforgotinfo->customer_id);
    	    if($customerforgotinfo->live>time()){
    	        if(md5($customerinfo->email)==$_GET['user']){
    	            $form=new FormChangepwd();
    	            $this->render('changpwd',array("model"=>$form));
    	        } 
    	    }
    	    else{
    	        echo "<script>location.href='".Yii::app()->baseUrl."/index/index</script>";//超时
    	    }
    	    if(!empty($_POST)){
    	        $salt=$this->create_salt();
    	        $result=$customer->updateAll(array(
    	            'password'=>$this->ppb_password_hash($_POST['FormChangepwd']['password'], $salt),
    	            'salt'=>$salt   	            	
    	        ),
    	            'id='.$customerforgotinfo->customer_id
    	            );
//     	        $customerinfo->password=$_POST['FormChangepwd']['password'];
//     	        $return=$customerinfo->save(false);
    	        if($result){
    	            $customerforgotinfo->live=0;
    	            $customerforgotinfo->modify=time();
    	            $return_forgot=$customerforgotinfo->save();
    	            if($return_forgot){
    	                echo "<script>location.href='".Yii::app()->baseUrl."/login/successpwd';</script>";
    	            }
    	            
    	        }
    	        else{
    	            echo "<script>location.href='".Yii::app()->baseUrl."/index/index</script>";//修改失败
    	        }
    	    }
	    }else{
	        echo "<script>location.href='".Yii::app()->baseUrl."/login/index';</script>";//CODE不正确
	    }
	    
	}
	/**
	 * 密码加密机制
	 * @param unknown $password
	 * @param unknown $salt
	 * @return string
	 */
	function ppb_password_hash ($password, $salt)
	{
	    return md5(md5($password) . $salt);
	}
	/**
	 * 生成随机码
	 * @return string
	 */
	function create_salt()
	{
	    $rand = md5(rand(2, 99999999));
	    $output = substr($rand, 0, 3);
	
	    return $output;
	}
	public function actionSuccess(){
	    $this->render('success');
	}
	public function actionSuccesspwd(){
	    $this->render('successpwd');
	}
}